package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.pojo.Order;
import com.qf.pojo.OrderItem;
import com.qf.pojo.Product;
import com.qf.utils.DruidJdbcUtils;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author 高圆圆
 * @date 2023/6/6 17:38
 * 订单的数据访问接口实现
 */
public class OrderDaoImpl implements OrderDao {
    /**
     * 添加订单数据
     * @param order 订单实体
     */
    @Override
    public void insertOrder(Order order) throws SQLException {
        //写法:自动提交方式:更新
        /*QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        String sql  = "insert into orders  values(?,?,?,?,?,?,?,?)" ;
        int count = qr.update(sql,
                order.getOid(),
                order.getOrdertime(),
                order.getTotal(),
                order.getState(),
                order.getAddress(),
                order.getName(),
                order.getTelephone(),
                order.getUser().getUid());*/

        //手动提交
        QueryRunner qr = new QueryRunner() ;
        String sql  = "insert into orders  values(?,?,?,?,?,?,?,?)" ;
        int count = qr.update(DruidJdbcUtils.getConnection(),sql, //三个参数:参1:连接对象 参数2:sql 参数3:实际参数
                order.getOid(),
                order.getOrdertime(),
                order.getTotal(),
                order.getState(),
                order.getAddress(),
                order.getName(),
                order.getTelephone(),
                order.getUser().getUid());

    }

    /**
     * 添加订单项数据
     * @param orderItem 订单项实体
     */
    @Override
    public void insertOrderItem(OrderItem orderItem) throws SQLException {

        /*QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        String sql  = "insert into orderitem  values(?,?,?,?,?)" ;
        int count = qr.update(sql,
                orderItem.getItemid(),
                orderItem.getCount(),
                orderItem.getSubtotal(),
                orderItem.getProduct().getPid(),
                orderItem.getOrder().getOid());*/

        QueryRunner qr = new QueryRunner() ;
        String sql  = "insert into orderitem  values(?,?,?,?,?)" ;
        int count = qr.update(DruidJdbcUtils.getConnection(),sql,
                orderItem.getItemid(),
                orderItem.getCount(),
                orderItem.getSubtotal(),
                orderItem.getProduct().getPid(),
                orderItem.getOrder().getOid());

    }

    /**
     * 查找订单的总计数
     * @param uid  条件就是用户uid
     * @return 返回总条数
     */
    @Override
    public int getTotalCount(String uid) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "SELECT COUNT(oid) FROM orders WHERE uid = ?" ;
        Object obj = qr.query(sql, new ScalarHandler<>(), uid);
        int totalCount = Integer.parseInt(String.valueOf(obj)) ;
        return totalCount;
    }

    /**
     * 查询订单分页数据
     * @param uid 用户uid
     * @param currentPage 当前页码
     * @param pageSize   每页显示的条数
     * @return  返回订单列表数据(包含订单项的数据---包含商品数据)
     */
    @Override
    public List<Order> selectOrderByPage(String uid, int currentPage, int pageSize) throws SQLException, InvocationTargetException, IllegalAccessException {
        QueryRunner qr  = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        String sql = "SELECT * FROM orders WHERE uid = ? ORDER BY ordertime DESC LIMIT ?,? " ; //分页查询订单数据,订单属于哪个用户条件
        //查询
        List<Order> list = qr.query(sql, new BeanListHandler<>(Order.class), uid, (currentPage - 1) * pageSize, pageSize);
        //遍历所有订单
        for(Order order :list){

            //sql--->封装订单的数据--->包含了多个订单项以及订单项包含的商品
            sql = "SELECT * FROM orderitem oi,product p WHERE oi.`pid` = p.`pid`  AND oi.`oid` = ? " ;
            //将订单项和商品的数据查询出来---多行多列的数据---封装Map(将这些实体存储在List集合中)
            List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler(), order.getOid());
            //遍历List集合 获取到每一行的订单项和商品数据
            for(Map<String,Object> map:mapList){
                //map---自己封装订单项
                OrderItem orderItem = new OrderItem() ;
                //BeanUtils工具---populate(实体对象,map集合)
                BeanUtils.populate(orderItem,map) ;

                //封装商品
                Product product = new Product() ;
                BeanUtils.populate(product,map);

                //将商品数据封装到订单项中
                orderItem.setProduct(product) ;

                //从订单实体中获取List<OrderItem>
                List<OrderItem> items = order.getItems();
                items.add(orderItem) ; //将订单项添加到订单集合中

            }
        }
        return list;
    }

    /**
     * 订单数据访问接口查询订单实体
     * @param oid 订单编号
     * @return 返回订单实体
     */
    @Override
    public Order selectOrderByOid(String oid) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        String sql = "select * from orders where oid = ?" ;
        Order order = qr.query(sql, new BeanHandler<>(Order.class), oid);
        return order;
    }

    /**
     * 订单数据访问接口 更新订单
     * @param order 订单实体
     */
    @Override
    public void update(Order order) throws SQLException {
       QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
       String sql = "update orders set state = ? where oid = ?" ;
        int count = qr.update(sql, order.getState(), order.getOid());
    }
}
